package com.controller;

import com.controller.vo.ResultVO;
import com.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.Impl.UserInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static com.utils.CreateUserDir.generateUserDirName;

/**
 * ClassName: UserInfoController
 * Description:
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/05/23 15:21
 */
@RestController
public class UserInfoController extends BaseController {
    @Autowired
    private UserInfoServiceImpl userInfoService;

    @GetMapping("/getAllUnAuditUserInfo")
    public ResultVO<PageInfo<User>> getAllUnAuditUserInfo(@RequestParam(defaultValue = "1") int pageNum,
                                                          @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<User> allUnAuditUserInfo = userInfoService.getAllUnAuditUserInfo(pageNum, pageSize);
        return success(allUnAuditUserInfo);
    }

    @PostMapping("/auditUserInfo")
    public ResultVO<String> auditUserInfo(@RequestParam("accountId") Integer accountId,
                                          @RequestParam("auditStatus") Integer auditStatus) {
        userInfoService.auditUserInfo(accountId, auditStatus);
        return success();
    }

    @GetMapping("/getUserInfoByAccountId")
    public ResultVO<User> getUserInfoByAccountId(@RequestParam("accountId") Integer accountId) {
        return success(userInfoService.selectUserInfoByAccountId(accountId));
    }

    @PostMapping("/updateUserInfoByAccountId")
    public ResultVO<String> updateUserInfoByAccountId(@RequestBody User userInfo,
                                                      @RequestPart("files") MultipartFile[] files) throws IOException {
        //1. 设置用户头像的默认存储路径
        String baseDir = "D:\\upload\\user\\avatar\\img\\";
        Path basePath = Paths.get(baseDir);

        //2. 生成专属文件夹
        String userDirName = generateUserDirName(basePath, userInfo.getNickname());
        Path userDir = basePath.resolve(userDirName);

        //3. 如果不存在目录，则创建
        if (!Files.exists(userDir)) {
            Files.createDirectories(userDir);
        }

        //4. 如果不为空,保存所有文件到该目录下
        if (files != null){
            List<String> addFiles = new ArrayList<>();
            for (MultipartFile file : files) {
                if (!file.isEmpty()){
                    String fileName = file.getOriginalFilename();
                    assert fileName != null;
                    Path filePath = userDir.resolve(fileName);
                    file.transferTo(filePath);
                    addFiles.add(filePath.toString()); // 将文件路径添加到列表中
                }
            }
            //5. 将路径添加进 UserInfo 对象中
            userInfo.setAvatar(userDir.toString());
        } else {
            userInfo.setAvatar(null);
        }


        userInfoService.updateUserInfoByAccountId(userInfo);
        return success();
    }
}
